<?php

class EasemobGroupSeeder extends Seeder {

    public function run() 
    {
        // firstly I need to remove the group members that are not existant anymore
		$snsGroups = SNSGroup::all();
		foreach($snsGroups as $snsGroup) {
		    $members = SNSGroupMember::where('group_id', $snsGroup->id)->get();
		    foreach($members as $member) {
		        $user = User::find($member->user_id);
		        if (!$user || !Easemob::getPass($user)) {
		            $member->delete();
		            Log::info(sprintf("Successfully deleted sns group member %s", $member->id));
		        }
		    }

		    $user = User::find($snsGroup->created_by);
		    if (!$user || !Easemob::getPass($user)) {
		        $snsGroup->delete();
		        Log::info(sprintf("Owner of this sns group does not exist %s", $snsGroup->created_by));
		    }
		}
        
        // then I can sync groups
    	$easemobGroupNames = $this->getEasemobGroupNames();
		$query = SNSGroup::whereNotIn('group_name',$easemobGroupNames);
		$groups = $query->get();
		$num = 0;
		foreach($groups as $group) {
	        $group->load('members');
	        $groupArr = $group->toArray();
	
	        $members = [];
	        foreach($groupArr['members'] as $member) {
	            $members[] = $member['user_id'];
	        }
	
	        $groupOption = array(
	            'groupname' => $groupArr['group_name'],
	            'desc' => '律团',
	            'approval' => false,
	            'public' => false,
	            'owner' => (string)$groupArr['created_by'],
	            'members' => $members
	        );
	        
    		try{
    		    $result = new Easemob();
    		    $result_re = $result->createGroups($groupOption);
    		    $dagg = json_decode($result_re);
    		    if(isset($dagg->error)){
    		        Log::info(sprintf("Error %s", $result_re));
    				throw new Exception($dagg->error);
    		    }
    		    
        		if(isset($dagg->data->groupid)){
        		    $group->easemob_id = $dagg->data->groupid;
        		    $group->save();
        		}
    		}catch (Exception $ex){
    		    Log::error($ex);
    		}
	        
	        Log::info(sprintf("Successfully added %s easemob groups: %s", ++$num, json_encode($groupOption)));
		}

		// update easemob_id with the group ids from easemob
		$result = new Easemob();
		$groupList = $result->chatGroups();
		$groupList = json_decode($groupList, true);
		foreach($groupList['data'] as $group) {
			$groupName = $group['groupname'];
			$groupId = $group['groupid'];
			$sns_group = SNSGroup::where('group_name', $groupName)->first();
			Log::info(" " . $groupName . $groupId);
			if ($sns_group) {
			    $sns_group->easemob_id = $groupId;
			    $sns_group->save();
			    Log::info(sprintf("Successfully updated easemob group: %s", $groupName));
			} else {
			    Log::info(sprintf("Easemob group does not exist: %s", $groupName));
			}
		}
		
    }

	/*
	 * 环信服务器用户
	 */
	public function getEasemobGroupNames() 
	{
		$result = new Easemob();
		$groupList = $result->chatGroups();
		$groupNames = [];
		$groupList = json_decode($groupList, true);
		foreach($groupList['data'] as $group) {
			$groupNames[] = $group['groupname'];
		}
		return $groupNames;
	}
}